home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / NUMBERS.SWG / 0061_LongInt to Hex in BASM.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  2KB  |  48 lines

  1.   From: Ryan Thompson                                Read: Yes    Replied: No
  2.  
  3. { I have an Integer to hex in pascal with ASM: }
  4.  
  5. Function HexOf(I : Longint) : String; Assembler;
  6.   Asm
  7.     jmp   @1                         { Skip table }
  8.   @0:
  9.     db    '0123456789ABCDEF'
  10.   @1:
  11.     cld                              { Clear direction flag }
  12.     les   di,@Result                 { ES:DI = Function return data }
  13.     mov   ax,$0008                   { Set String size }
  14.     stosb                            { in the output, }
  15.     mov   cx,4                       { Loop 4x for four bytes }
  16.     mov   si,3
  17.   @2:
  18.     mov   al,byte [I+si]             { Load AL with next byte }
  19.     dec   si
  20.     push  si                         { SAVE index register! }
  21.     mov   bl,al                      { Load DL... }
  22.     mov   dl,bl                      { and BL, }
  23.     and   bx,$00F0                   { prepare and ... }
  24.     {$IFOPT G+}
  25.     shr   bx,4
  26.     {$ELSE}
  27.     shr   bx,1                       { convert BL to high nybble only, }
  28.     shr   bx,1
  29.     shr   bx,1                       { 8088-compatible }
  30.     shr   bx,1
  31.     {$ENDIF}
  32.     and   dx,$000F                   { and DL to low nybble only. }
  33.     mov   si,bx                      { move high nybble into index, }
  34.     mov   al,byte [cs:@0+si]         { read Character for that nybble, }
  35.     stosb                            { Write high nybble }
  36.     mov   si,dx                      { move low nybble into index, }
  37.     mov   al,byte [cs:@0+si]         { read Character for that nybble, }
  38.     stosb                            { Write low nybble }
  39.     pop   si                         { RESTORE index register! }
  40.     loop  @2                         { Dec CX; Loop if CX <> 0 }
  41.   End;
  42.  
  43. {
  44.   It's not wonderfully written code, but it does work.  Spits out an 8-digit
  45. hex string from a longint.  You could either delete the unneeded parts of the
  46. string or make a version that doesn't do all four bytes.
  47. }
  48.